package com.shopping;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.servlet.http.HttpSession;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portlet.shopping.model.ShoppingCart;
import com.liferay.portlet.shopping.service.ShoppingCartLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.CartItem;
import com.slayer.service.ShoppingCartExtLocalServiceUtil;
import com.util.CartUtil;

/**
 * Portlet implementation class ShoppingPortlet
 */

public class MiniCartPortlet extends MVCPortlet {

	@Override
	public void init() throws PortletException {
		super.init();
		addProcessActionSuccessMessage = false;
	}

	public void setPreferences(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		boolean isCheckoutPage = ParamUtil.getBoolean(actionRequest,
				"isCheckoutPage", false);

		String portletResource = ParamUtil.getString(actionRequest,
				"portletResource");

		if (Validator.isNotNull(portletResource)) {
			PortletPreferences preferences;
			try {
				preferences = PortletPreferencesFactoryUtil.getPortletSetup(
						actionRequest, portletResource);
				preferences.setValue("isCheckoutPage",
						(String.valueOf(isCheckoutPage)));

				preferences.store();
			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void removeFromCart(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		HttpSession session = PortalUtil.getHttpServletRequest(actionRequest)
				.getSession();

		Map<Long, CartItem> items = (HashMap<Long, CartItem>) session
				.getAttribute("USER_CART_ITEMS");

		long itemId = ParamUtil.getLong(actionRequest, "itemId");
		if (Validator.isNotNull(items)) {
			items.remove(itemId);
		}

		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);

		// if the user is signed in
		if (themeDisplay.isSignedIn()) {

			ShoppingCart shoppingCart = CartUtil.getCart(themeDisplay);
			shoppingCart.setModifiedDate(new Date());

			try {
				ShoppingCartLocalServiceUtil.updateShoppingCart(shoppingCart);
			} catch (SystemException e) {
				e.printStackTrace();
			}

			ShoppingCartExtLocalServiceUtil.removeItemFromCart(
					shoppingCart.getCartId(), itemId);

		}
	}

	@SuppressWarnings("unchecked")
	public void emptyCart(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		HttpSession session = PortalUtil.getHttpServletRequest(actionRequest)
				.getSession();

		Map<Long, CartItem> items = (HashMap<Long, CartItem>) session
				.getAttribute("USER_CART_ITEMS");
		if (Validator.isNotNull(items)) {
			items.clear();
		}

		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);

		// if the user is signed in
		if (themeDisplay.isSignedIn()) {
			ShoppingCart shoppingCart = CartUtil.getCart(themeDisplay);

			shoppingCart.setItemIds(StringPool.BLANK);
			shoppingCart.setModifiedDate(new Date());
			try {
				ShoppingCartLocalServiceUtil.updateShoppingCart(shoppingCart);
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			ShoppingCartExtLocalServiceUtil
					.removeCart(shoppingCart.getCartId());
		}
	}

	public void checkOut(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);

		if (themeDisplay.isSignedIn()) {
			actionResponse.sendRedirect(themeDisplay.getURLPortal()
					+ "/checkout");
		} else {
			actionResponse.sendRedirect(themeDisplay.getURLSignIn());
		}
	}
}